﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LIMS.SqlServerDAL
{
    /// <summary>
    /// 留言服务类
    /// By:田玉粮
    /// 2013.3.19
    /// </summary>
    public class MessageService : LIMS.IDAL.IMessage
    {
        LIMSDataContext DB = new LIMSDataContext();
        /// <summary>
        /// 将留言写入数据库
        /// </summary>
        /// <param name="message">带添加的留言对象</param>
        public Boolean Add(LIMS.Model.Messages mes)
        {
            TB_Message messTmp = new TB_Message()
            {
                mesStuNo = mes.mesStuNo,
                mesTitle = mes.mesTitle,
                mesDetail = mes.mesDetail,
                mesTime = DateTime.Now,
                // 0 表示未处理，  1表示已处理
                mesStatus = "0",
                mesAdminId = string.Empty,
                mesReply = string.Empty
            };
            DB.TB_Message.InsertOnSubmit(messTmp);
            DB.SubmitChanges();
            return true;
        }

        /// <summary>
        /// 根据ID删除留言
        /// </summary>
        /// <param name="message">带指定删除留言ID的对象</param>
        /// <returns></returns>
        public Boolean Remove(LIMS.Model.Messages message)
        {
            TB_Message messTmp = new TB_Message()
            {
                mesId = Int32.Parse(message.mesID)
            };
            DB.TB_Message.DeleteOnSubmit(messTmp);
            DB.SubmitChanges();
            return true;
        }

        /// <summary>
        /// 根据留言ID获取该留言学生的姓名
        /// </summary>
        /// <param name="mesID">留言ID</param>
        /// <returns></returns>
        public String FindStuNameByID(String mesID)
        {
            return DB.TB_Message.Where(s => s.mesId == Int32.Parse(mesID)).Single().TB_Student.stuName;
        }

        /// <summary>
        /// 获取所有留言
        /// </summary>
        /// <returns></returns>
        public List<LIMS.Model.Messages> FindAll()
        {
            List<LIMS.Model.Messages> listTmp = new List<Model.Messages>();
            DB.TB_Message.Select(s => s).ToList().ForEach((item) =>
            {
                Model.Messages mesTmp = new Model.Messages()
                {
                    mesStuNo = item.mesStuNo,
                    mesID = item.mesId.ToString(),
                    mesTitle = item.mesTitle,
                    mesDetail = item.mesDetail,
                    mesTime = item.mesTime,
                    mesStatusCode = item.mesStatus,
                    mesAdminID = item.mesAdminId,
                    mesReply = item.mesReply
                };
                listTmp.Add(mesTmp);
            });
            return listTmp;
        }

        /// <summary>
        /// 根据留言的状态获取留言
        /// </summary>
        /// <param name="statusCode">留言的状态码</param>
        /// <returns></returns>
        public List<LIMS.Model.Messages> FindByStatus(String statusCode)
        {
            List<LIMS.Model.Messages> listTmp = new List<Model.Messages>();
            DB.TB_Message.Where(w => w.mesStatus == statusCode).Select(s => s).ToList().ForEach((item) =>
            {
                Model.Messages mesTmp = new Model.Messages()
                {
                    mesStuNo = item.mesStuNo,
                    mesID = item.mesId.ToString(),
                    mesTitle = item.mesTitle,
                    mesDetail = item.mesDetail,
                    mesTime = item.mesTime,
                    mesStatusCode = item.mesStatus,
                    mesAdminID = item.mesAdminId,
                    mesReply = item.mesReply
                };
                listTmp.Add(mesTmp);
            });
            return listTmp;
        }

        /// <summary>
        /// 根据留言ID，更新回复消息
        /// </summary>
        /// <param name="message">带指定更新留言ID的对象</param>
        /// <returns></returns>
        public Boolean UpdateByReply(LIMS.Model.Messages message)
        {
            TB_Message messTmp = DB.TB_Message.SingleOrDefault(sd => sd.mesId == Int32.Parse(message.mesID));
            messTmp.mesAdminId = messTmp.mesAdminId;
            messTmp.mesReply = messTmp.mesReply;
            //表示已经回复
            messTmp.mesStatus = "1";
            DB.SubmitChanges();
            return true;
        }

        /// <summary>
        /// 通过留言ID获取该数据信息
        /// </summary>
        /// <param name="mesID">留言ID</param>
        /// <returns></returns>
        public Model.Messages FindByID(String mesID)
        {
            Model.Messages message = new Model.Messages();
            var mesTmp = DB.TB_Message.Where(w => w.mesId == Int32.Parse(mesID)).Select(s => s).FirstOrDefault();
            if (mesTmp != null)
            {
                message.mesDetail = mesTmp.mesDetail;
                message.mesTime = mesTmp.mesTime;
                message.mesID = mesTmp.mesId.ToString();
                message.mesTitle = mesTmp.mesTitle;
                message.mesStuNo = mesTmp.mesStuNo;
                message.mesStatusCode = mesTmp.mesStatus;
                message.mesReply = mesTmp.mesReply;
                message.mesAdminID = mesTmp.mesAdminId;
            }
            return message;
        }

    }

}
